package com.aaa.controller;

import com.aaa.entity.Shop;
import com.aaa.service.ShopService;
import com.aaa.util.DefaultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/shop")

public class ShopController {

    @Autowired
    private ShopService shopService;


    @RequestMapping("/listAll")
    public List<Shop> listAll(){
        List<Shop> list = shopService.list();
        return list;
    }

    @RequestMapping("/saveOrUpdate")
    public DefaultMsg saveOrUpdate(Shop shop){
        DefaultMsg defaultMsg = new DefaultMsg();
        boolean b = shopService.saveOrUpdate(shop);
        if (!b){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
        }
        return defaultMsg;
    }

    @RequestMapping("/delete")
    public boolean delete(Shop shop){
        boolean save = shopService.removeById(shop.getSid());
        return save;
    }
    @RequestMapping("/upload")
    public DefaultMsg upload(MultipartFile file){
        System.out.println(1111111);
        DefaultMsg defaultMsg=new DefaultMsg();
        //声明要上传的文件夹
        String dir="D:/upload_374/head/";
        //生成唯一的保存文件名
        String saveName= UUID.randomUUID().toString();
        File saveFile=new File(dir+saveName);
        try{
            file.transferTo(saveFile);
            defaultMsg.setTarget(saveName);
        } catch (IOException e) {
            e.printStackTrace();
            defaultMsg.setSuccess(0);
            defaultMsg.setError("上传失败");
        }

        //file.transferTo();
        return defaultMsg;
    }
}
